import { on, off } from "@/utils/dom";
//开始按
function touchstart(e, el, binding) {
	el._timer = setTimeout(() => longPress(e, el, binding), 500); //这里设置定时器，定义长按500毫秒触发长按事件，时间可以自己改，个人感觉500毫秒非常合适
	return false;
}
//手释放，如果在500毫秒内就释放，则取消长按事件，此时可以执行onclick应该执行的事件
function touchend(e, el) {
	clearTimeout(el._timer); //清除定时器
	if (el._timer != 0) {
		//这里写要执行的内容（尤如onclick事件）
	}
	return false;
}
//如果手指有移动，则取消所有事件，此时说明用户只是要移动而不是长按
function touchmove(e, el) {
	clearTimeout(el._timer); //清除定时器
	el._timer = 0;
}

//真正长按后应该执行的内容
function longPress(e, el, binding) {
	const handler = typeof binding.value === "function" ? binding.value : binding.value.handler;
	//执行长按要执行的内容，如弹出菜单
	handler && handler(e);
}
export default {
	inserted(el, binding) {
		const onTouchstart = (e) => touchstart(e, el, binding);
		const onTouchend = (e) => touchend(e, el, binding);
		const onTouchmove = (e) => touchmove(e, el, binding);
		el._touchstart = onTouchstart;
		el._touchend = onTouchend;
		el._touchmove = onTouchmove;

		on(el, "touchstart", el._touchstart);
		on(el, "touchend", el._touchend);
		on(el, "mousedown", el._touchstart);
		on(el, "mouseup", el._touchend);
		// on(el, "touchmove", el._touchmove);
	},
	unbind(el) {
		off(el, "touchstart", el._touchstart);
		off(el, "touchend", el._touchend);
		off(el, "mousedown", el._touchstart);
		off(el, "mouseup", el._touchend);
		// off(el, "touchmove", el._touchmove);
		delete el._touchstart;
		delete el._touchend;
		// delete el._touchmove;
	}
};
